<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\DatatreeApi;
use Shop\Api\ArticleApi;
use Admin\Api\VPostInfoApi;
use Admin\Model\DatatreeModel;
use Uclient\Api\UserApi;

class ArticleController extends  AdminController{

    public function index(){

        $result  = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>'6152')));
        if($result['status']){
            $this->assign("article_cates",$result['info']);
        }
        $name = I("name",'');
        $cate = I("cate",'');

        //分页时带参数get参数
        $params = array(
            'name'=>$name,
        );

        $map = array();

//	$map['post_author'] = UID;
//  @hzboye010 zhouzhou 2016/05/12
        $map['title'] = array('like', '%'.$name.'%');
        if(!empty($cate)){
            $params['cate'] = $cate;
            $this->assign("cur_cate",$cate);
            //仅查询了下一级子类目
            $result  = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>$cate)));
            if($result['status'] && $result['info']){
                foreach ($result['info'] as $v) {
                    $cate .= ','.$v['id'];
                }
                $map['type'] = ['in',$cate];
            }else{
                $map['type'] = $cate;
            }
        }

        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));


        $result = apiCall(ArticleApi::QUERY,array($map,$page,$order,$params));
        //
        if($result['status']){
            $this->assign('name',$name);
            $this->assign('show',$result['info']['show']);
            $this->assign('list',$result['info']['list']);
            $this->display();
        }else{
            LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
    }


    public function add(){
        $title=I('title');
        if(empty($title)){



            $this->display();
        }else{
            $type = I('type');
            $no_type=array('6153','6154','6155','6156','6157','6158','6159');
            if( in_array($type,$no_type)) $this->error('类型选择错误');
            $title = I('title');
            $create_time = time();
            $url = I('url','');
            $author=I('author','admin');
            if(empty($type)) $this->error('缺少信息');
            if(empty($title)) $this->error('缺少信息');
            if(empty($url)) $this->error('缺少信息');
            if(empty($author)) $this->error('缺少信息');
            $entity = array(
                'type'=>$type,
                'title'=>$title,
                'create_time'=>$create_time,
                'url'=>$url,
                'author'=>$author,
            );
            $result = apiCall(ArticleApi::ADD, array($entity));
            if(!$result['status']){
                $this->error($result['info']);
            }
            $this->success("操作成功！",U('Admin/Article/index'));

        }
    }



    public function edit(){
        $id = I('id',0);
        if(IS_GET){
            $result = apiCall("Admin/Post/getInfo", array(array("id"=>$id)));
            if(!$result['info']){
                $this->error($result['info']);
            }
            $this->assign("vo",$result['info']);
            $this->display();
        }else{
            $post_category = I('post.post_category',20);

            $start_time = I('post.start_time','');
            $end_time = I('post.end_time','');
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $user = session("global_user");
            $jump_url = I('post.jump_url','');

            $entity = array(
                'jump_url'=>$jump_url,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'main_img'=>I('post.main_img',''),
                'post_category'=>$post_category,
                'post_author'=>UID,
                'post_content'=>I('post_content',''),
                'post_excerpt'=>I('post_excerpt',''),
                'post_title'=>I('post_title',''),
                'post_status'=>I('post_status','draft'),
                'comment_status'=>I('commen_status','closed'),
            );
            $entity['post_author_name'] = $user['username'];
            $result = apiCall(PostApi::SAVE_BY_ID, array($id,$entity));

            if(!$result['status']){
                $this->error($result['info']);
            }

            $this->success("保存成功！",U('Admin/Post/index'));

        }
    }

    public function delete(){
        $id = I('id',0);
        $result = apiCall(ArticleApi::DELETE, array(array("id"=>$id)));
        if(!$result['status']){
            $this->error($result['info']);
        }
        $this->success("删除成功！");

    }

}
